
import axios from 'axios';
import Cfg from './config.js';
import { showLoadingToast, showConfirmDialog, showImagePreview, showToast, Uploader, closeToast } from 'vant';

// 对于数据请求的封装
const instance = axios.create({
    baseURL: Cfg.root,
    timeout: Cfg.utime,
    headers: {
      'Content-Type': 'application/json',

    },
  });


//   拦截器
instance.interceptors.request.use(
    config => {
      const token = localStorage.getItem('token');
      if (token) {
        config.headers.Authorization = `Bearer ${token}`;
      }
      
      return config;
    },
    error => {
      return Promise.reject(error);
    }
  );
  
  instance.interceptors.response.use(
    response => {
      return response.data;
    },
    error => {
      // showToast('请求成功');
      if (error.response) {
       
        switch (error.response.status) {
          case 401:
            // 处理未授权
            break;
          case 404:
            // 处理未找到
            break;
          default:
            // 处理其他错误
        }
      }
      return Promise.reject(error);
    }
  );


  const http = {
    get: (url, params) => instance.get(url, { params }),
    post: (url, data) => instance.post(url, data),
    put: (url, data) => instance.put(url, data),
    delete: (url) => instance.delete(url),
  };
  
  export default http

 








